package com.hhit.zhgd.controller

import com.hhit.zhgd.domain.dto.StopTaskReqDTO
import com.hhit.zhgd.domain.dto.UpdateTaskReqDTO
import com.hhit.zhgd.domain.response.BaseResp
import com.hhit.zhgd.service.ZhgdCronJobService
import com.hhit.zhgd.task.JobScheduler
import com.hhit.zhgd.task.factory.AutowiringSpringBeanJobFactory
import jakarta.annotation.Resource
import org.slf4j.LoggerFactory
import org.springframework.validation.BindingResult
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
import javax.validation.Valid

@RestController
class ScheduledTaskController {
    private val log= LoggerFactory.getLogger(ScheduledTaskController::class.java)

    @Resource
    private val jobScheduler: JobScheduler? = null

    @PostMapping("/pauseTask")
    fun pauseTask(@RequestBody @Valid stopTaskReqDTO: StopTaskReqDTO, bindingResult: BindingResult):BaseResp<Any?> {
        return try {
            //判断有没有异常错误,如果有则返回默认消息
            if (bindingResult.hasErrors()) {
                val defaultMessage = bindingResult.fieldError?.defaultMessage
                return BaseResp.fail(defaultMessage)
            }
            jobScheduler?.pauseTask(stopTaskReqDTO.taskName)
            BaseResp.success()
        } catch (e: Exception) {
            log.error("stopTask error:", e)
            BaseResp.fail("定时任务停止失败!" + e.message)
        }
    }


    /**
     * @description 修改定时任务cron表达式
     * @author qianchengcheng
     * @date 2024/4/3 14:52
     */
    @PostMapping("updateTask")
    fun updateTask(@RequestBody updateTaskReqDTO: @Valid UpdateTaskReqDTO?, bindingResult: BindingResult): BaseResp<Any?> {
        return try {
            //判断有没有异常错误,如果有则返回默认消息
            if (bindingResult.hasErrors()) {
                val defaultMessage = bindingResult.fieldError?.defaultMessage
                return BaseResp.fail(defaultMessage)
            }
            jobScheduler?.updateTaskCronExpression(
                updateTaskReqDTO?.taskName!!,
                updateTaskReqDTO?.cronExpression
            )
            BaseResp.success()
        } catch (e: java.lang.Exception) {
            log.error("updateTask error:", e)
            BaseResp.fail("定时任务修改失败!" + e.message)
        }
    }

    /**
     * @description 停用定时任务
     * @author qianchengcheng
     * @date 2024/4/3 14:56
     */
    @PostMapping("stopTask")
    fun stopTask(@RequestBody stopTaskReqDTO: @Valid StopTaskReqDTO?, bindingResult: BindingResult): BaseResp<Any?> {
        return try {
            //判断有没有异常错误,如果有则返回默认消息
            if (bindingResult.hasErrors()) {
                val defaultMessage = bindingResult.fieldError?.defaultMessage
                return BaseResp.fail(defaultMessage)
            }
            jobScheduler?.stopTask(stopTaskReqDTO?.taskName)
            BaseResp.success()
        } catch (e: java.lang.Exception) {
            log.error("stopTask error:", e)
            BaseResp.fail("停用定时任务异常:" + e.message)
        }
    }

    /**
     * @description 恢复暂停的定时任务
     * @author qianchengcheng
     * @date 2024/4/3 15:14
     */
    @PostMapping("resumeTask")
    fun resumeTask(@RequestBody stopTaskReqDTO: @Valid StopTaskReqDTO?, bindingResult: BindingResult): BaseResp<Any?> {
        return try {
            //判断有没有异常错误,如果有则返回默认消息
            if (bindingResult.hasErrors()) {
                val defaultMessage = bindingResult.fieldError?.defaultMessage
                return BaseResp.fail(defaultMessage)
            }
            jobScheduler?.resumeTask(stopTaskReqDTO?.taskName)
            BaseResp.success()
        } catch (e: java.lang.Exception) {
            log.error("resumeTask error:", e)
            BaseResp.fail("恢复暂停的定时任务异常:" + e.message)
        }
    }
}